package com.feihong.txgw.tx_precenter;

import android.util.Log;

import com.feihong.txgw.inter.contract.integral.IIntegralContract;
import com.feihong.txgw.model.bean.integral.IntegralBackEntity;
import com.feihong.txgw.tx_model.IntegralMainModel;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName IntegralMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/3 16:41
 * @Describe
 * @Change
 */
public class IntegralMainPrecenter extends IIntegralContract.AbIntegralMainPrecenter {
    public static IntegralMainPrecenter create() {
        return new IntegralMainPrecenter();
    }

    private int mCurrentPageNum = 1;

    private boolean isFullPage = true;
    @Override
    public IIntegralContract.IIntegralMainModel getModel() {
        return IntegralMainModel.create();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void loadBackIntegralData(String cacheKey, String requestType) {
        int pageNum = mCurrentPageNum;
        mIModel.getBackIntegralData(cacheKey, requestType, pageNum).subscribe(new Observer<IntegralBackEntity>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(IntegralBackEntity entity) {
                if (entity.getCode() == 2021) {
                    mIView.notData();
                } else if (entity.getCode() == 200) {
                    if(isFullPage && mCurrentPageNum <= 1 ){
                        isFullPage = entity.getData().getPage_list().size() > 7;
                    }
                    mCurrentPageNum++;
                    mIView.setBackIntegralData(isFullPage,entity.getData().getPage_list());
                } else {
                    mIView.dataOtherError(entity.getMsg());
                }
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public void loadIntegralDataFirst(String cacheKey, String requestType) {
        mCurrentPageNum = 1;
        isFullPage = true;
        loadBackIntegralData(cacheKey, requestType);
    }
}
